﻿using Styx.Common;
using Styx.CommonBot;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;

namespace MIBHunter
{
    public partial class Classname
    {
        #region BeastMasteryRotation

        private Composite BeastMasteryRotation()
        {
            return new PrioritySelector(
                new Action(delegate
                    {
                        if (MIBSettings.Instance.AutoTarget && !Me.Mounted &&
                            Me.CurrentTarget != null && UnitAttack != null &&
                            Me.CurrentTarget != UnitAttack &&
                            UnitAttack.Combat && //Make sure don't aggro more
                            (Me.CurrentTarget.Distance - 5 > UnitAttack.Distance ||
                             !Me.CurrentTarget.InLineOfSpellSight ||
                             !Me.CurrentTarget.IsAlive ||
                             !Me.CurrentTarget.Combat && Me.Combat))
                        {
                            Logging.Write(LogLevel.Diagnostic, "Select new Target");
                            UnitAttack.Target();
                        }

                        //target the lowest threat unit
                        if (MIBSettings.Instance.AutoTarget && Me.CurrentTarget != null &&
                            Me.CurrentTarget.IsValid && GetUnitLowThreat() &&
                            _unitLowThreat.IsValid && Me.CurrentTarget != _unitLowThreat &&
                            Me.CurrentTarget.ThreatInfo.RawPercent < _unitLowThreat.ThreatInfo.RawPercent)
                        {
                            _unitLowThreat.Target();
                            Logging.Write(LogLevel.Diagnostic, "Switch to Lowest Threat Target");
                        }

                        //target the lowest threat unit
                        if (MIBSettings.Instance.AutoTarget && Me.CurrentTarget == null &&
                            GetUnitLowThreat() &&
                            _unitLowThreat.IsValid)
                        {
                            _unitLowThreat.Target();
                            Logging.Write(LogLevel.Diagnostic, "Target  Lowest Threat Unit");
                        }

                        //Hold dps on Dungeon or Raid
                        if ((Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                            Me.CurrentTarget != null &&
                            !Me.CurrentTarget.Combat)
                        {
                            return RunStatus.Success;
                        }

                        return RunStatus.Failure;
                    }),
                //But first, facing target
                new Decorator(
                    ret =>
                    MIBSettings.Instance.AutoFace &&
                    Attackable(Me.CurrentTarget, 10) &&
                    !Me.Mounted &&
                    !IsOverrideModeOn &&
                    !Blacklist.Contains(Me.CurrentTarget, BlacklistFlags.All) &&
                    (Me.CurrentTarget.IsTargetingMeOrPet ||
                     Me.CurrentTarget.IsTargetingMyPartyMember ||
                     Me.CurrentTarget.IsTargetingMyRaidMember) &&
                    !Me.IsFacing(Me.CurrentTarget),
                    new Action(delegate
                        {
                            Me.CurrentTarget.Face();
                            return RunStatus.Failure;
                        })
                    ),
                //Starting the movement right here
                MovementMoveStop(ret => Me.CurrentTarget, 3),
                MovementMoveToMelee(ret => Me.CurrentTarget),
                //DPS Rotation Here
                SerpentSting(),
                SetAutoAttack()
                );
        }

        #endregion
    }
}